package com.qf.service.impl;

import com.qf.dto.DeveloperDto;
import com.qf.mapper.DeveloperMapper;
import com.qf.mapper.ProjectMapper;
import com.qf.model.Developer;
import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.DeveloperService;
import com.qf.utils.UUIDUtils;
import com.qf.vo.DeveloperVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeveloperServiceImpl implements DeveloperService {
    @Autowired
    private DeveloperMapper developerMapper;
    @Autowired
    private ProjectMapper projectMapper;
    //分页查询+模糊查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        //当前页结果
        List<Developer> rows = developerMapper.findPage((CurrentPage-1)*PageSize, PageSize,queryString);
        //总记录数
        Long count = developerMapper.count(queryString);
        pageResult.setTotal(count);
        pageResult.setRows(rows);
        return pageResult;
    }

    //新建开发商并绑定工程
    @Override
    @Transactional
    public boolean add(DeveloperVo developerVO) {
        Developer developer = new Developer();
        developer.setDid(UUIDUtils.uuid());
        developer.setDname(developerVO.getDname());
        developer.setUpdatetime(new Date());
        //新增开发商
        int i = developerMapper.add(developer);
        //绑定工程
        int[] pids = developerVO.getPids();
        for (int pid : pids) {
            int i1 = projectMapper.bindDeveloper(pid, developer.getDid());
            if (i1 <=0){
                return false;
            }
        }
        if (i>0){
            return true;
        }else {
            return false;
        }
    }


    //根据pid查询developer
    @Override
    public DeveloperDto findById(String did) {
        DeveloperDto developerDto = developerMapper.findById(did);
        List<Project> projects = projectMapper.findByDid(did);
        int[] pids = null;
        if (ObjectUtils.isNotEmpty(projects)){
            pids= new int[projects.size()];
            for (int i = 0; i < projects.size(); i++) {
                pids[i] = projects.get(i).getPid();
            }
        }

        developerDto.setPids(pids);
        return developerDto;
    }

    //修改developer
    @Override
    @Transactional
    public int edit(DeveloperVo developerVo) {
        developerVo.setUpdatetime(new Date());
        //取消所有绑定了对应did的project的did
        projectMapper.cancelBindContractorByDid(developerVo.getDid());
        int[] pids = developerVo.getPids();
        for (int pid : pids) {
            //developer绑定选中的project
            projectMapper.bindDeveloper(pid,developerVo.getDid());
        }
        //修改developer
        int i = developerMapper.editOne(developerVo);

        return i;
    }

    //删除developer
    @Override
    @Transactional
    public int delete(String did) {
        int i = developerMapper.delete(did);
        if (i>0){
            //取消所有绑定了对应did的project的did
            int i1 = projectMapper.cancelBindContractorByDid(did);
            return i1;
        }
        return 0;
    }
}
